#include "StdAfx.h"
#include "MainDlg.h"
#include "WEGlobal.h"
#include "WceUiLayerCtrl.h"
#include "WceUiDlg.h"
#include "Engine.h"
CMainDlg::CMainDlg(void)
{
}

CMainDlg::~CMainDlg(void)
{
}

void CMainDlg::OnLButtonDown(UINT nFlags, POINT point)
{
	CRect rc(0, 0, 100, 100);
	if (rc.PtInRect(point))
	{
		EndDialog(0);
	}
}


LRESULT CMainDlg::OnCommand(UINT uCommand, WPARAM wParam, LPARAM lParam)
{
	if (CMD_WE_I_CLICK == uCommand)
	{
		OnCmd((CWceUiLayer*)wParam);
	}

	return 0;
}

void CMainDlg::OnCmd(CWceUiLayer* pLayer)
{
#if 1
	LPCTSTR layer_name=pLayer->GetName();
	Engine *pengine=Engine::GetInstance();
	ILauncher *launcher=pengine->GetLauncher();
	pIApp launched_app=NULL;
	while (launcher->NextApp())
	{
		pIApp cur_app=launcher->GetApp();
		LPCTSTR launch_name=cur_app->GetAppLaunchName();
		if (wcscmp(layer_name,launch_name)==0)
		{
			launched_app=cur_app;
		}
	}
	if (launched_app)
	{

		CWceUiDlg* pDlg = GetLayerCtrl()->CreateDlg(launched_app->GetAppName());
		ASSERT(NULL != pDlg);
		pDlg->DoModal();
		GetLayerCtrl()->DeleteLayer(pDlg);
		pDlg = NULL;

	}
	else
	{
		OutputDebugString(L"no launched_app\r\n");
	}
#endif

#if 0
	if (pLayer->IsEqualName(L"settings"))
	{
		CWceUiDlg* pDlg = GetLayerCtrl()->CreateDlg(L"CSettingsDlg");
		ASSERT(NULL != pDlg);
		pDlg->DoModal();
		GetLayerCtrl()->DeleteLayer(pDlg);
		pDlg = NULL;
	}else if(pLayer->IsEqualName(L"photos"))
	{
		TCHAR szPath[MAX_PATH] = {0};
		WEGetExePath(szPath);
		_tcscat(szPath, L"AppPhoto.exe");
		WEWinExec(szPath);
	}
	else if (pLayer->IsEqualName(L"music"))
	{
		CWceUiDlg* pDlg = GetLayerCtrl()->CreateDlg(L"CRadioDlg");
		ASSERT(NULL != pDlg);
		pDlg->DoModal();
		GetLayerCtrl()->DeleteLayer(pDlg);
		pDlg = NULL;

	}
#endif
}

void CMainDlg::OnInitDialog()
{
	m_pFrame = (CWEILayer*)GetChildByName(L"bottom_frame");
	ASSERT(NULL != m_pFrame);
	ASSERT(m_pFrame->IsKindOf(WCEUI_CLASS_NAME(CWEILayer)));
	m_pFrame->SetSlideSpeed(1, 1, 1);
	m_pFrame->SetBlockSlide(TRUE);
}
